package org.example.com.service.impl;

import org.example.com.mapper.UserMapper;
import org.example.com.pojo.User;
import org.example.com.service.UserService;
import org.example.com.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    public User findByPhoneOrName(String loginKey) {
        return userMapper.findByPhoneOrName(loginKey);
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public User findByName(String name) {
        return userMapper.findByName(name);
    }

    @Override
    public void updatePassword(Integer id, String newPassword) {
        userMapper.updatePassword(id, newPassword);
    }

    @Override
    public void updateName(Integer id, String newName) {
        userMapper.updateName(id, newName);
    }
    public void updateAvatar(String avatarUrl) {
        // 拿到当前登录用户的id
        Map<String, Object> claims = ThreadLocalUtil.get();
//        System.out.println(claims);
        int id = (int) claims.get("id");

        // 更新当前用户的头像地址
        userMapper.updateAvatar(avatarUrl, id);
    }


}
